package com.lxs.doc.rocess.demo;



import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class DemoDoc {
    public static void main(String[] args) {
        String  filePath = "";
        try (FileInputStream fis = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(fis)) {

            // 获取文档范围
            Range range = document.getRange();

            // 解析段落大纲
            parseParagraphsOutline(range);

            // 解析样式大纲
            parseStylesOutline(document);

            // 解析书签大纲（如果有）
            parseBookmarksOutline(document);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void parseParagraphsOutline(Range range) {
        System.out.println("=== 段落大纲 ===");

        for (int i = 0; i < range.numParagraphs(); i++) {
            Paragraph paragraph = range.getParagraph(i);

            // 获取段落文本
            String text = paragraph.text().trim();
            if (text.isEmpty()) continue;

            // 获取段落样式信息
            int styleIndex = paragraph.getStyleIndex();
            int paragraphLevel = paragraph.getLvl();
            boolean isHeading = isHeadingStyle(paragraph);

            System.out.printf("段落 %d [级别: %d, 样式: %d, 标题: %s]%n",
                    i, paragraphLevel, styleIndex, isHeading);
            System.out.println("内容: " + text);
            System.out.println("---");
        }
    }

    private static void parseStylesOutline(HWPFDocument document) {
        System.out.println("=== 样式大纲 ===");

        StyleSheet styleSheet = document.getStyleSheet();
        if (styleSheet != null) {
            for (int i = 0; i < styleSheet.numStyles(); i++) {
                StyleDescription style = styleSheet.getStyleDescription(i);
                if (style != null) {
                    System.out.printf("样式 %d: %s (基准样式: %d)%n",
                            i, style.getName(), style.getBaseStyle());

                    // 输出样式详细信息
                    if (style.getPAP() != null) {
                        System.out.printf("  段落级别: %d%n", style.getPAP().getIlvl());
                    }
                }
            }
        }
    }

    private static void parseBookmarksOutline(HWPFDocument document) {
        System.out.println("=== 书签大纲 ===");

        Bookmarks bookmarks = document.getBookmarks();
        if (bookmarks != null) {
            for (int i = 0; i < bookmarks.getBookmarksCount(); i++) {
                Bookmark bookmark = bookmarks.getBookmark(i);
                System.out.printf("书签: %s (开始: %d, 结束: %d)%n",
                        bookmark.getName(), bookmark.getStart(), bookmark.getEnd());
            }
        }
    }

    private static boolean isHeadingStyle(Paragraph paragraph) {
        // 判断是否为标题样式（简单判断）
        int styleIndex = paragraph.getStyleIndex();
        String text = paragraph.text().trim();

        // 根据样式索引或文本特征判断
        return styleIndex >= 1 && styleIndex <= 9 || // 通常标题样式在1-9
                text.length() < 100 && !text.contains("。") && // 标题通常较短
                        paragraph.getLvl() > 0; // 有缩进级别
    }






    public static List<OutlineItem> extractOutlineHierarchy(String filePath) {
        List<OutlineItem> outline = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(fis)) {

            Range range = document.getRange();
            Stack<OutlineItem> stack = new Stack<>();

            for (int i = 0; i < range.numParagraphs(); i++) {
                Paragraph paragraph = range.getParagraph(i);
                String text = paragraph.text().trim();

                if (text.isEmpty()) continue;

                int level = paragraph.getLvl();
                OutlineItem item = new OutlineItem(
                        i, text, level, paragraph.getStyleIndex()
                );

                // 构建层次结构
                while (!stack.isEmpty() && stack.peek().level >= level) {
                    stack.pop();
                }

                if (!stack.isEmpty()) {
                    stack.peek().addChild(item);
                } else {
                    outline.add(item);
                }

                stack.push(item);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return outline;
    }

    // 大纲项类
    static class OutlineItem {
        int index;
        String text;
        int level;
        int styleIndex;
        List<OutlineItem> children;

        public OutlineItem(int index, String text, int level, int styleIndex) {
            this.index = index;
            this.text = text;
            this.level = level;
            this.styleIndex = styleIndex;
            this.children = new ArrayList<>();
        }

        public void addChild(OutlineItem child) {
            children.add(child);
        }

        public void print(String indent) {
            System.out.printf("%s[%d] L%d: %s%n",
                    indent, index, level, text);
            for (OutlineItem child : children) {
                child.print(indent + "  ");
            }
        }
    }
}
